package cn.ranko.handler;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhujun on 2017/9/16.
 */
public class RKExceptionHandler  implements HandlerExceptionResolver {


    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ex.printStackTrace();

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("ex", ex);

        Boolean isAjax = !org.springframework.util.StringUtils.isEmpty(request.getHeader("x-requested-with"));
        model.put("isAjax", isAjax);

        // 根据不同错误转向不同页面
        if(ex instanceof UnauthorizedException) {
            //用户在没有权限的情况下，shiro对应无权限
            return new ModelAndView("error/unauthorized", model);
        }else {
            return new ModelAndView("error/default", model);
        }


    }
}